﻿#include "tcpserver.h"
#include "ui_tcpserver.h"
#include<QUdpSocket>
#include<QHostAddress>
#include<QAbstractSocket>
#include<QMessageBox>
#include<QDebug>

TcpServer::TcpServer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TcpServer)
{
    ui->setupUi(this);

    this->setWindowTitle("Server:9999");
    this->setFixedSize(561,445);
    connect(ui->lineEdit,&QLineEdit::returnPressed,
            ui->buttonSend,&QAbstractButton::click,Qt::UniqueConnection);

    udpSocket = new QUdpSocket(this);
    udpSocket->bind(9999);
    connect(udpSocket,&QUdpSocket::readyRead,this,&TcpServer::dealrecvMsg);

    tcpServer = new QTcpServer(this);
    tcpSocket = new QTcpSocket(this);
    tcpServer->listen(QHostAddress::Any,9999);
    connect(tcpServer,&QTcpServer::newConnection,this,&TcpServer::dealNewconnection);
    connect(tcpSocket,&QTcpSocket::readyRead,this,&TcpServer::tcpRecvMsg);
}

TcpServer::~TcpServer()
{
    delete ui;
}

/*************************************************
  Description:    udp接收信息处理函数
  return:         void
  Called By:
  Auther:         林锦涛
  Time:           2021-1-31 14:31:48
*************************************************/
void TcpServer::dealrecvMsg()
{
    char buf[1024] = {0};
    quint16 port;
    QHostAddress cliAddr;

    qint64 len = udpSocket->readDatagram(buf,sizeof(buf),&cliAddr,&port);
    //qDebug()<<buf<<port<<cliAddr;
    if(len>0)
    {
        //格式化{193.68.2.2:8888}aaaa
        QString str = QString("[%1: %2] %3")
                .arg(cliAddr.toString())
                .arg(port)
                .arg(buf);

        ui->textEdit->append(str);
    }
}

/*************************************************
  Description:    发送消息按钮函数
  return:         void
  Called By:
  Auther:         林锦涛
  Time:           2021-1-31 14:31:48
*************************************************/
void TcpServer::on_buttonSend_clicked()
{
    //先获取对方的IP和端口
    QString ip= "127.0.0.1";
    qint16 port = 8888;

    QString str = ui->lineEdit->text();
    ui->lineEdit->clear();
    //string 转字节数组 直接.toutf-8
    udpSocket->writeDatagram(str.toUtf8(),QHostAddress(ip),port);
}

/*************************************************
  Description:    tcp处理新连接函数
  return:         void
  Called By:
  Auther:         林锦涛
  Time:           2021-1-31 14:31:48
*************************************************/
void TcpServer::dealNewconnection()
{
    tcpSocket = tcpServer->nextPendingConnection();
    ui->buttonSend->setEnabled(true);
    ui->labelServer->setText(QStringLiteral("连接成功!!"));
    qDebug()<<"success";

    QString ip = tcpSocket->peerAddress().toString();
    quint16 port = tcpSocket->peerPort();

    QString str = QString("%1 : %2").arg(ip).arg(port);
    ui->textEdit->setText(str);

    connect(tcpSocket,&QTcpSocket::readyRead,this,&TcpServer::tcpRecvMsg);
    connect(tcpSocket,&QTcpSocket::disconnected,this,&TcpServer::dealDisconnect);
}

/*************************************************
  Description:    tcp接收信息处理函数
  return:         void
  Called By:
  Auther:         林锦涛
  Time:           2021-1-31 14:31:48
*************************************************/
void TcpServer::tcpRecvMsg()
{
    QByteArray buf = tcpSocket->readAll();
    qDebug()<<buf<<"receive success!!";
    ui->textEdit->setText(buf);
    ui->labelServer->setText(QStringLiteral("文件接收成功!!"));
}

/*************************************************
  Description:    断开连接处理函数
  return:         void
  Called By:
  Auther:         林锦涛
  Time:           2021-1-31 14:31:48
*************************************************/
void TcpServer::dealDisconnect()
{
    tcpSocket->close();
    ui->labelServer->setText(QStringLiteral("断开连接!!"));
}

/*************************************************
  Description:    重写关闭事件函数
  return:         void
  Called By:
  Auther:         林锦涛
  Time:           2021-1-31 14:31:48
*************************************************/
void TcpServer::closeEvent(QCloseEvent *e)
{
    if(QMessageBox::question(this,
                             "Quit",
                             "Are you sure to close?",
                             QMessageBox::Yes,QMessageBox::No)
            == QMessageBox::Yes)
    {
        e->accept();
        tcpSocket->close();
    }
    else
    {
        e->ignore();
    }
}


